
package tfoundation.ui;

import java.awt.BorderLayout;
import java.awt.BufferCapabilities;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.util.ConcurrentModificationException;
import java.util.ListIterator;

import javax.swing.JPanel;

import tfoundation.feed.BodyDisplay;
import tfoundation.feed.DisplayFeed;

public class UI
{
	private static final String DEF_FONT = "courrier new";

	private static final Font FONT = new Font(DEF_FONT, 2, 18);

	private final DisplayFeed feed;

	private final JPanel main;

	private final JPanel controlPane;
	private final JPanel displayPane;
	private final JPanel menuPane;
	private final JPanel listPane;
	private final JPanel selectionPane;
	private final JPanel bottomPane;

	public UI(DisplayFeed feed)
	{

		Dimension dim = null;

		this.feed = feed;

		this.main = new JPanel(new BorderLayout());
		this.controlPane = new FundationPane();
		this.displayPane = new DisplayPane();
		this.menuPane = new MenuPane();
		this.listPane = new ListPane();
		this.selectionPane = new SelectionPane();
		this.bottomPane = new BottomBar();

		// control

		this.controlPane.setPreferredSize(new Dimension(400, 0));
		this.controlPane.add(this.listPane, BorderLayout.CENTER);

		this.selectionPane.setPreferredSize(new Dimension(0, 250));
		this.controlPane.add(this.selectionPane, BorderLayout.SOUTH);

		// menu
		this.menuPane.setPreferredSize(new Dimension(0, 50));

		// bottom
		this.bottomPane.setPreferredSize(new Dimension(0, 50));

		// assemblage final
		this.main.add(this.displayPane, BorderLayout.CENTER);
		this.main.add(this.controlPane, BorderLayout.WEST);
		this.main.add(this.menuPane, BorderLayout.NORTH);
		this.main.add(this.bottomPane, BorderLayout.SOUTH);
	}

	public JPanel getMainPane()
	{
		return this.main;
	}

	public void refresh()
	{

	}

	private class DisplayPane extends FundationPane
	{
		private DisplayPane()
		{
		}

		@Override
		public void paintComponent(Graphics g)
		{
			// BufferStrategy myStrategy = new BufferStrategy();

			// super.paintComponent(g);

			final int width = this.getWidth();
			final int height = this.getHeight();
			final int halfWidth = width / 2;
			final int halfHeight = height / 2;

			final int GUIDE_SIZE = 30;

			ListIterator<BodyDisplay> ite;
			BodyDisplay bodyDisplay;

			int x;
			int y;
			int left;
			int right;
			int top;
			int bottom;

			// Dessin des guides
			g.setColor(Color.WHITE);
			g.drawLine(0, halfHeight, GUIDE_SIZE, halfHeight);
			g.drawLine(halfWidth - GUIDE_SIZE, halfHeight, halfWidth + GUIDE_SIZE, halfHeight);
			g.drawLine(width - GUIDE_SIZE, halfHeight, width, halfHeight);
			g.drawLine(halfWidth, 0, halfWidth, GUIDE_SIZE);
			g.drawLine(halfWidth, halfHeight - GUIDE_SIZE, halfWidth, halfHeight + GUIDE_SIZE);
			g.drawLine(halfWidth, height - GUIDE_SIZE, halfWidth, height);

			g.setColor(Color.GREEN);
			ite = UI.this.feed.getBodies();
			while (ite.hasNext())
			{

				{
					bodyDisplay = ite.next();

					x = halfWidth + bodyDisplay.x;
					left = halfWidth + bodyDisplay.left;
					right = halfWidth + bodyDisplay.right;

					y = halfHeight - bodyDisplay.y;
					top = halfHeight - bodyDisplay.top;
					bottom = halfHeight - bodyDisplay.bottom;

					g.drawOval(left, top, bodyDisplay.width, bodyDisplay.height);
					g.drawString(bodyDisplay.getBroadcastName(), left, bottom + 16);
				}

			}

		}
	}

	private class ListPane extends FundationPane
	{
		private ListPane()
		{
		}

		@Override
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);

		}
	}

	private class SelectionPane extends FundationPane
	{
		private SelectionPane()
		{
		}

		@Override
		public void paintComponent(Graphics g)
		{

		}
	}

	private class MenuPane extends FundationPane
	{
		private MenuPane()
		{
		}

		@Override
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);

		}
	}

	private class BottomBar extends FundationPane
	{
		private BottomBar()
		{
		}

		@Override
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);

		}
	}

	private class BuffSt extends BufferStrategy
	{

		@Override
		public boolean contentsLost()
		{
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean contentsRestored()
		{
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public BufferCapabilities getCapabilities()
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Graphics getDrawGraphics()
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void show()
		{
			// TODO Auto-generated method stub

		}
	}
}
